//
//  LoginViewController.swift
//  TJJPT
//
//  Created by FENGYL on 2017/5/16.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit
import SVProgressHUD

class LoginViewController: UIViewController {
    
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var rememberUsernameButton: UIButton!
    @IBOutlet weak var confirmButton: UIButton!
    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setUpView()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - 初始化
    func setUpView() {
        
        
        // MARK: 设置rememberPasswordButton
        self.rememberUsernameButton.setTitle(NSLocalizedString(LOCALIZABLE_STRING_REMEMBER_USER_NAME, comment: ""), for: .normal)
        //是否选中
        if (UserDefaults.standard.value(forKey: USER_DEFAULTS_IS_REMEMBER_USER_NAME) != nil) {
            
            self.rememberUsernameButton.isSelected = (UserDefaults.standard.value(forKey: USER_DEFAULTS_IS_REMEMBER_USER_NAME) as? Bool)!
            
        }

        
        
        
        // MARK: 设置userNameTextField
        self.userNameTextField.placeholder = NSLocalizedString(LOCALIZABLE_STRING_USER_NAME, comment: "")
        if self.rememberUsernameButton.isSelected {
            
            self.userNameTextField.text = UserDefaults.standard.value(forKey: USER_DEFAULTS_USER_NAME) as? String
            
        }
        
        
        // MARK: 设置passwordTextField
        self.passwordTextField.placeholder = NSLocalizedString(LOCALIZABLE_STRING_PASSWORD, comment: "")
        
        
        
        // MARK: 设置confirmButton
        self.confirmButton.setTitle(NSLocalizedString(LOCALIZABLE_STRING_CONFIRM, comment: ""), for: .normal)
        
    }
    
    // MARK: - RememberUserNameButton点击事件
    @IBAction func isRememberUserName(_ sender: UIButton) {
        
        self.rememberUsernameButton.isSelected = !self.rememberUsernameButton.isSelected
        
        
        
    }
    
    // MARK: - confirmButton点击事件
    @IBAction func login(_ sender: UIButton) {
        
        // MARK: 判断userNameTextField,passwordTextField是否为空
        if !((self.userNameTextField.text?.characters.count)! > 0 && (self.passwordTextField.text?.characters.count)! > 0) {
            
            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_USER_NAME_OR_PASSWORD_IS_EMPTY, comment: ""))
            
            SVProgressHUD.dismiss(withDelay: 1)
            
            return
            
        }
        
        SVProgressHUD.show()
        
        NetworkCommand.login(userName: self.userNameTextField.text!, password: self.passwordTextField.text!, success: { (response) in
            
            NetworkCommand.GetDevicesByAccount(id: nil, clientType: nil, tokenPass: nil, success: { (response) in
                
                if ((response as? NSArray) == nil) {
                    
                    SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_LOGIN_FAILED, comment: ""))
                    SVProgressHUD.dismiss(withDelay: 1)
                    
                    return
                    
                }
                
                let array:NSArray = response as! NSArray
                
                if array.count > 0{
                    
                    let dict:NSDictionary = array[0] as! NSDictionary
                    
                    UserModel.userModel.selectedDeviceSerialNumber = (dict.value(forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER) as? String) ?? ""
                    
                }
                
                UserDefaults.standard.set(self.rememberUsernameButton.isSelected, forKey: USER_DEFAULTS_IS_REMEMBER_USER_NAME)
                
                UserDefaults.standard.set(true, forKey: USER_DEFAULTS_IS_LOGON)
                
                UserModel.userModel.lastConectedDate = Date.init()
                
                SVProgressHUD.dismiss()
                
                if (self.presentingViewController != nil) {
                    
                    super.dismiss(animated: true, completion: nil)
                    
                    
                }else{
                    
                    let storyboard = UIStoryboard.init(name: "MainViewController", bundle: Bundle.main)
                    
                    let mainTabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController")
                    
                    self.present(mainTabBarController, animated: true, completion: nil)
                    
                }
                
            }, failure: { (Error) in
                
                SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_LOGIN_FAILED, comment: ""))
                SVProgressHUD.dismiss(withDelay: 1)
                
            })
            
            
            
        }) { (Error) in
            
            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_LOGIN_FAILED, comment: ""))
            SVProgressHUD.dismiss(withDelay: 1)
            
        }
        
    }
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
